package edu.zut.main.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ImgUpLoad {
    public static void main(String[] args) {

        File file = new File("D:\\21-22-1\\mobile203-204\\java203-204\\a.jpg");
        System.out.println(file.exists());
    }
}

//模拟将图片从客户端上传到服务器
//模拟 多个客户端上传文件到服务器   + 线程控制
class ImgClient{
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8888);

        File file = new File("D:\\21-22-1\\mobile203-204\\java203-204\\a.jpg");
        //输出流    基于socket
        OutputStream os = socket.getOutputStream();
        //本地输入流
        FileInputStream is = new FileInputStream(file);
        byte[] buffer = new byte[1024];
        int len;
        //一边读本地的图片，一边向服务器上传
        while ((len = is.read()) != -1){
            os.write(buffer,0,len);
        }

        is.close();
        os.close();
        socket.close();

    }
}


class ImgServer{
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("等待客户端连接.....");
        Socket socket = serverSocket.accept();
        System.out.println("连接已建立");
        //基于socket读取数据
        InputStream is = socket.getInputStream();

//        File file = new File("./b.jpg");
//        if (!file.exists())
//            file.createNewFile();
        //服务器端的输出流，用于将读到的数据保存到服务器本地
        FileOutputStream os = new FileOutputStream(new File("./b.jpg"));
        int len;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        os.flush();
        os.close();
        is.close();
        socket.close();
        serverSocket.close();



    }
}





